using FireBillBook.Core.Repos;
using FireBillBook.Core.Repos.Interfaces;
using FireBillBook.Core.Services;
using FireBillBook.Core.Services.Interfaces;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using System.Collections.Generic;
using NSwag;
using FireBillBook.Core.Extensions;

namespace FireBillBook
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
        {
            Configuration = configuration;
            Env = hostEnvironment;
        }

        static string DefaultPolicyName = "LittleBlogClient";

        public IConfiguration Configuration { get; }
        public IHostEnvironment Env { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddLogging(config =>
            {
                config.ClearProviders();
                config.AddNLog("NLog.config");
            });
            services.AddDbContext<BillDbContext>(options =>
            {
                options.UseNpgsql(Configuration.GetConnectionString("Default"));
                if (!Env.IsDevelopment())
                {
                    var logFactory = new LoggerFactory();
                    logFactory.AddProvider(new NLogLoggerProvider());
                    options.UseLoggerFactory(logFactory);
                }
            });

            services.AddScoped<IConsumptionRecordRepo, ConsumptionRecordRepo>();
            services.AddScoped<IBillingService, BillingService>();

            services.AddOpenApiDocument(config =>
            {
                config.UseControllerSummaryAsTagDescription = true;
                config.GenerateExamples = true;
            });


            services.AddAutoMapper(typeof(FireBillBook.Core.Models.BillingProfile));

            var allowedOrigins = new List<string>();
            Configuration.GetSection("AllowedOrigins").Bind(allowedOrigins);

            services.AddCors(setup =>
            {
                setup.AddPolicy(DefaultPolicyName, policy =>
                {
                    policy.WithOrigins(allowedOrigins.ToArray()).AllowAnyMethod().AllowAnyHeader();
                });
            });

            services.AddScoped<IImportService, ImportService>();
            services.AddScoped<IBillingSourceDataRepo, BillingSourceDataRepo>();
            services.AddScoped<IPlatformStore, PlatformStore>();

            services.AddPlatformServiceFactory();

            services.ConfigurePlatformService(builder =>
            {
                builder.AddPlatform<AliPayService>();
                builder.AddPlatform<WechatPayService>();
                builder.AddPlatformInfos();
            });

            
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseOpenApi();
                app.UseSwaggerUi3();
            }

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseCors(DefaultPolicyName);

            app.UseForwardedHeaders(new ForwardedHeadersOptions()
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            //app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
